package cn.fl.ref.fncprjbscinfo.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.asspackinfo.transform.AssPackInfoVOTransform;
import cn.fl.ref.asspackinfo.transform.FncPrjPayRltvInfoVOTransform;
import cn.fl.ref.asspackinfo.vo.AssPackInfoVO;
import cn.fl.ref.asspackinfo.vo.FncPrjPayRltvInfoVO;
import cn.fl.ref.fncprjbscinfo.service.IFncPrjBscInfoService;
import cn.fl.ref.fncprjbscinfo.transform.FncPrjBscInfoVOTransform;
import cn.fl.ref.fncprjbscinfo.vo.FncPrjBscInfoVO;
import cn.fl.ref.fncprjdrawmon.transform.FncPrjDrawMonVOTransform;
import cn.fl.ref.fncprjdrawmon.vo.FncPrjDrawMonVO;
import cn.fl.ref.fncprjfstpyinfo.transform.FncPrjDetailVOTransform;
import cn.fl.ref.fncprjfstpyinfo.transform.FncPrjFstpyInfoVOTransform;
import cn.fl.ref.fncprjfstpyinfo.vo.FncPrjFstpyInfoVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.asspackinfo.service.IAssPackInfoApiService;
import fl.ref.fncprjbscinfo.condition.FncPrjBscInfoQueryCondition;
import fl.ref.fncprjbscinfo.dto.FncPrjBscInfoDTO;
import fl.ref.fncprjbscinfo.service.IFncPrjBscInfoApiService;
import fl.ref.fncprjdrawmon.service.IFncPrjDrawMonApiService;
import fl.ref.fncprjfstpyinfo.condition.FncPrjFstpyInfoQueryCondition;
import fl.ref.fncprjfstpyinfo.constant.FncPrjFstpyInfoConstant;
import fl.ref.fncprjfstpyinfo.dto.FncPrjFstpyInfoDTO;
import fl.ref.fncprjfstpyinfo.service.IFncPrjFstpyInfoApiService;
/**
 * @Description: 项目基本信息
 * @Description:业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 * 服务实现层
 */
@Service
public class FncPrjBscInfoServiceImpl implements IFncPrjBscInfoService {

	private static final Logger logger = LoggerFactory.getLogger(IFncPrjBscInfoService.class);
	
	@Autowired
	private IAssPackInfoApiService assPackInfoApiService;
	
	@Autowired
	private IFncPrjBscInfoApiService  fncPrjBscInfoApiService;

	@Autowired
	private IFncPrjFstpyInfoApiService fncPrjFstpyInfoApiService;
	
	@Autowired
	private IFncPrjDrawMonApiService fncPrjDrawMonApiService;
	
	@Override
	public FncPrjBscInfoVO selectById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		FncPrjBscInfoDTO dto = this.fncPrjBscInfoApiService.selectById(id);
		return FncPrjBscInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncPrjBscInfoVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		//系统日志-有用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPrjBscInfo数据.", CurrentThreadContext.getCurrentUserName());
		
		//系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPrjBscInfo数据.");
		return this.fncPrjBscInfoApiService.add(FncPrjBscInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.fncPrjBscInfoApiService.removeById(id);
	}

	@Override
	public boolean modify(FncPrjBscInfoVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.fncPrjBscInfoApiService.modify(FncPrjBscInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		return this.fncPrjBscInfoApiService.searchListPage(page);
	}
	

	@Override
	public List<FncPrjBscInfoVO> searchList(FncPrjBscInfoQueryCondition condition) {
		// TODO Auto-generated method stub
		return FncPrjBscInfoVOTransform.toVOList(this.fncPrjBscInfoApiService.searchList(condition))  ;
	}	

	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException{
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!",CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.fncPrjBscInfoApiService.addFlowStart(flowStartParamsDTO);
	}

	/**
	 * 添加资产包
	 * @param assPackInfo
	 * @param payRltvInfo
	 * @return
	 * @throws BaseException
	 */
	@Override
	public Long copyPack(AssPackInfoVO assPackInfo) throws BaseException {
		long mainId = 0;
		try {

			//保存资产包信息
				mainId = this.assPackInfoApiService.addAssPackInfo(AssPackInfoVOTransform.toDTO(assPackInfo));
		
				
		} catch (Exception e) {
			throw new BaseException(e.getMessage());
		}
		
		return mainId;
	}
	
	@Override
	public Long copyPayRltvInfo(FncPrjPayRltvInfoVO vo) throws BaseException {
		long mainId = 0;
		try {

			mainId = this.assPackInfoApiService.addPayRltvInfo(FncPrjPayRltvInfoVOTransform.toDTO(vo));				
		
				
		} catch (Exception e) {
			throw new BaseException(e.getMessage());
		}
		
		return mainId;
	}

	@Override
	public boolean modifyfpy(List<FncPrjFstpyInfoVO> fncPrjFstpyInfoVOs) throws BaseException {
		if (null == fncPrjFstpyInfoVOs) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
           boolean ck=false;
		if (CollectionUtils.isNotEmpty(fncPrjFstpyInfoVOs)) {
			for (FncPrjFstpyInfoVO fncPrjFstpyInfoVO : fncPrjFstpyInfoVOs) {
				if (StringUtils.isEmpty(fncPrjFstpyInfoVO.getId())) {
					//-----------------陈君20171213防止网卡，多次点击重复录入---------------------
					/*逻辑不对，先注释掉 郭璇 2018-02-22
					 * FncPrjFstpyInfoQueryCondition fstqc=new FncPrjFstpyInfoQueryCondition();
					fstqc.setFncPrjId(fncPrjFstpyInfoVO.getFncPrjId());
					fstqc.setScmChrgId(fncPrjFstpyInfoVO.getScmChrgId());
					fstqc.setWthrDdctFstPay(1);
					List<FncPrjFstpyInfoDTO> fstlist=this.fncPrjFstpyInfoApiService.searchList(fstqc);
					if(fstlist != null && fstlist.size() >0) {
						for (FncPrjFstpyInfoDTO fstDTO : fstlist) {
							if(fstDTO != null && fstDTO.getId() != null) {
								this.fncPrjFstpyInfoApiService.removeById(fstDTO.getId());
							}
						}
					}*/
					
					//----------------------------------------------
					this.fncPrjFstpyInfoApiService.add(FncPrjFstpyInfoVOTransform.toDTO(fncPrjFstpyInfoVO));
				} else {
					this.fncPrjFstpyInfoApiService.modify(FncPrjFstpyInfoVOTransform.toDTO(fncPrjFstpyInfoVO));
				}
			}
			ck=true;
		}

		return ck;
	}
	
	@Override
	public boolean modifyTranmess(List<FncPrjDrawMonVO> fncPrjDrawMonVOs,Long prjid) throws BaseException {
		if (null == fncPrjDrawMonVOs) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
           boolean ck=false;
		if (CollectionUtils.isNotEmpty(fncPrjDrawMonVOs)) {
			this.fncPrjDrawMonApiService.removeById(prjid);
			for (FncPrjDrawMonVO fncPrjDrawMonVO : fncPrjDrawMonVOs) {
				//if (StringUtils.isEmpty(fncPrjDrawMonVO.getId())) {
					
					this.fncPrjDrawMonApiService.add(FncPrjDrawMonVOTransform.toDTO(fncPrjDrawMonVO));
				//} else {
				//	this.fncPrjDrawMonApiService.modify(FncPrjDrawMonVOTransform.toDTO(fncPrjDrawMonVO));
				//}
			}
			ck=true;
		}

		return ck;
	}

	@Override
	public Pager searchFlowPageById(Pager page) throws BaseException {
		// TODO Auto-generated method stub
		if (null == page)
			page = new Pager();
		return this.fncPrjBscInfoApiService.searchFlowPageById(page);
	}

	@Override
	public Pager searchInsUserListPage(Pager page) throws BaseException {
		// TODO Auto-generated method stub
		return this.fncPrjBscInfoApiService.searchInsUserListPage(page);
	}

	@Override
	public Pager searchAssUseListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		page=this.fncPrjBscInfoApiService.searchAssUseListPage(page);
		return page;
	}
	
	
}
